package com.laizhenghua.share.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laizhenghua.share.common.core.context.SecurityContextHolder;
import com.laizhenghua.share.common.core.domain.R;
import com.laizhenghua.share.order.api.constant.OrderStatus;
import com.laizhenghua.share.order.api.domain.entity.OrderInfo;
import com.laizhenghua.share.order.mapper.OrderInfoMapper;
import com.laizhenghua.share.order.service.OrderInfoService;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * TODO
 *
 * @Description
 * @Author laizh
 * @Date 2025/10/27 22:06
 **/
@Service
public class OderServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Override
    public R<OrderInfo> findNotFinishOrder() {
        Long userId = SecurityContextHolder.getUserId();
        LambdaQueryWrapper<OrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderInfo::getUserId, userId);
        queryWrapper.in(OrderInfo::getStatus, Arrays.asList(OrderStatus.ORDER_USE.getStatus(), OrderStatus.ORDER_UNPAID.getStatus()));
        queryWrapper.orderByDesc(OrderInfo::getId);
        queryWrapper.last("limit 1");
        return R.ok(baseMapper.selectOne(queryWrapper));
    }
}
